package edu.qqilin.springframework.test;

import cn.hutool.core.io.IoUtil;
import edu.qqilin.springframework.beans.PropertyValue;
import edu.qqilin.springframework.beans.PropertyValues;
import edu.qqilin.springframework.beans.factory.config.BeanDefinition;
import edu.qqilin.springframework.beans.factory.BeanFactory;
import edu.qqilin.springframework.beans.factory.config.BeanReference;
import edu.qqilin.springframework.beans.factory.support.DefaultListableBeanFactory;
import edu.qqilin.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import edu.qqilin.springframework.context.support.ClassPathXmlApplicationContext;
import edu.qqilin.springframework.core.io.DefaultResourceLoader;
import edu.qqilin.springframework.core.io.Resource;
import edu.qqilin.springframework.test.bean.UserDao;
import edu.qqilin.springframework.test.bean.UserService;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Description: new java files header..
 *
 * @author qqilin1213
 * @version 1.0
 * @date 2025/11/22 15:31
 */
public class ApiTest {
    @Test
    public void test_BeanFactory(){
        // 1.初始化 BeanFactory
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

        // 2. UserDao 注册
        beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class));

        // 3. UserService 设置属性[uId、userDao]
        PropertyValues propertyValues = new PropertyValues();
        propertyValues.addPropertyValue(new PropertyValue("uId","10001"));
        propertyValues.addPropertyValue(new PropertyValue("userDao",new BeanReference("userDao")));

        // 4. UserService 注入bean
        beanFactory.registerBeanDefinition("userService", new BeanDefinition(UserService.class,propertyValues));

        // 5. UserService 获取bean
        UserService userService = (UserService) beanFactory.getBean("userService");
        userService.queryUserInfo();
    }
    @Test
    public void test_xml() {
        // 1.初始化 BeanFactory
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:springPostProcessor.xml");

        // 2. 获取Bean对象调用方法
        UserService userService = applicationContext.getBean("userService", UserService.class);
        String result = userService.queryUserInfo();
        System.out.println("测试结果：" + result);
    }

}
